<template>
  <a-modal
    :title="title"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item label="角色名称">
          <a-input v-decorator="['name', {rules: [{required: true, min: 2, message: '请至少输入两个字符'}]}]" />
        </a-form-item>
        <a-form-item label="描述">
          <a-input v-decorator="['description']" />
        </a-form-item>
        <a-form-item label="菜单选择">
          <a-tree-select
            style="width: 100%"
            :tree-data="menuTreeData"
            tree-checkable
            :show-checked-strategy="SHOW_ALL"
            search-placeholder="Please select"
            v-decorator="['menu_tree']"
          ></a-tree-select>
        </a-form-item>
        <a-form-item label="方法选择">
          <a-tree-select
            style="width: 100%"
            :tree-data="actionTreeData"
            tree-checkable
            :show-checked-strategy="SHOW_ALL"
            search-placeholder="Please select"
            v-decorator="['action_tree']"
          ></a-tree-select>
        </a-form-item>
        <a-form-item>
          <a-input type="hidden" v-decorator="['id']">
          </a-input>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { TreeSelect } from 'ant-design-vue'
import pick from 'lodash.pick'
import { getTree as menuTree } from '@/api/menu'
import { getTree as actionTree } from '@/api/action'
const SHOW_ALL = TreeSelect.SHOW_ALL
// 表单字段
const fields = ['name', 'description', 'id', 'menu_tree', 'action_tree']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    },
    title: {
      type: String,
      default: () => ''
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      edit: true,
      form: this.$form.createForm(this),
      menuTreeData: [],
      actionTreeData: [],
      SHOW_ALL
    }
  },
  created () {
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  watch: {
    title: function (val) {
      if (val === '编辑角色') {
        this.edit = false
      } else {
        this.edit = true
      }
    }
  },
  mounted () {
      this.menuTree()
      this.actionTree()
  },
  methods: {
    menuTree () {
          menuTree().then((res) => {
          if (res.code === 0) {
              this.menuTreeData = res.data
          } else {
              this.$message.error(res.message)
          }
        }).catch((err) => {
          console.log(err)
        })
      },
      actionTree () {
          actionTree().then((res) => {
          if (res.code === 0) {
              this.actionTreeData = res.data
          } else {
              this.$message.error(res.message)
          }
        }).catch((err) => {
          console.log(err)
        })
      }
  }
}
</script>
